package com.xiaoq.sites.repository.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.coms.util.IdGeneratorUtils;
import com.xiaoq.sites.entity.SitesPointArEntity;
import com.xiaoq.sites.repository.ISitesPointArRepository;

@Repository
public class SitesPointArRepositoryImpl implements ISitesPointArRepository {
	
	@Autowired
	private Datastore datastore;

	private static final Logger LOG = LoggerFactory.getLogger(SitesPointArRepositoryImpl.class);
	
	@Override
	public List<SitesPointArEntity> findPointArList(String sitesId) {

		List<SitesPointArEntity> sitesPointArList = new ArrayList<>();
		Query<SitesPointArEntity> pointArQuery = datastore.find(SitesPointArEntity.class);

		if (StringUtils.isNotEmpty(sitesId)) {
			pointArQuery.field("sitesId").equal(sitesId);
			if (pointArQuery.count() != 0) {
				sitesPointArList = pointArQuery.asList();
			} else {
				LOG.error("没有这种景区会报空指针异常！");
			}

		} else {
			LOG.error("请传递一个准确的sitesId!");
		}
		return sitesPointArList;
	}

	@Override
	public boolean addPointAr(SitesPointArEntity sitesPointAr) {
		datastore.save(sitesPointAr);
		return true;
	}

	@Override
	public boolean deletePointAr(String id) {
		Query<SitesPointArEntity> pointArQuery = datastore.find(SitesPointArEntity.class).field(IdGeneratorUtils.IDGENERATOR).equal(id);
		datastore.delete(pointArQuery);
		return true;
	}

	@Override
	public SitesPointArEntity findById(String id) {
		SitesPointArEntity sitesPointAr = datastore.find(SitesPointArEntity.class).field(IdGeneratorUtils.IDGENERATOR).equal(id).get();
		return sitesPointAr;
	}
	

}
